package com.example.maoyanmovie.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import androidx.appcompat.app.AppCompatActivity;
import com.example.maoyanmovie.databinding.ActivityRegisterBinding;
import com.example.maoyanmovie.utils.ToastUtils;

public class RegisterActivity extends AppCompatActivity {

    private ActivityRegisterBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityRegisterBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initListeners();
    }

    private void initListeners() {
        binding.btnRegister.setOnClickListener(v -> register());

        binding.tvLogin.setOnClickListener(v -> finish());
    }

    private void register() {
        String username = binding.etUsername.getText().toString().trim();
        String phone = binding.etPhone.getText().toString().trim();
        String password = binding.etPassword.getText().toString().trim();
        String passwordConfirm = binding.etPasswordConfirm.getText().toString().trim();

        if (TextUtils.isEmpty(username)) {
            ToastUtils.showShort(this, "请输入用户名");
            return;
        }

        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort(this, "请输入手机号");
            return;
        }

        if (phone.length() != 11) {
            ToastUtils.showShort(this, "手机号格式不正确");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ToastUtils.showShort(this, "请输入密码");
            return;
        }

        if (password.length() < 6) {
            ToastUtils.showShort(this, "密码长度至少6位");
            return;
        }

        if (!password.equals(passwordConfirm)) {
            ToastUtils.showShort(this, "两次密码输入不一致");
            return;
        }

        // 模拟注册成功
        ToastUtils.showShort(this, "注册成功，请登录");
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}
